//
//  ProblemOffer38.swift
//  LeetCode
//
//  Created by 毕武侠 on 2022/3/19.
//  Copyright © 2022 zhulongiMac02. All rights reserved.
//

import UIKit

/*
 剑指 Offer 38. 字符串的排列
 输入一个字符串，打印出该字符串中字符的所有排列。
 你可以以任意顺序返回这个字符串数组，但里面不能有重复元素。
 示例:
     输入：s = "abc"
     输出：["abc","acb","bac","bca","cab","cba"]
 限制：
    1 <= s 的长度 <= 8
 */
class ProblemOffer38: NSObject {
    /*
     递归方法：
     "abc" = ("a" + permutation("bc")) + ("b" + permutation("ac")) + ("c" + permutation("ab"))
     */
    func permutation(_ s: String) -> [String] {
        if s.isEmpty {
            return []
        }
        if s.count == 1 {
            return [s]
        }
        if s.count == 2 {
            return [s, String(s.reversed())]
        }
        
        var list: [String] = []
        
        for (i, c) in s.enumerated() {
            var temp = s[s.startIndex..<s.index(s.startIndex, offsetBy: i)] + s[s.index(s.startIndex, offsetBy: i+1)..<s.endIndex]
            print(temp)
        }
        
        return list
    }
}
